package com.xspace.android.xafcommon.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式工具类
 * <p>
 * create time 2013-12-16 version 1.0.0.0 class ExpUtils.java
 *
 * @author mawen
 */
public class ExpUtils {
    /**
     * 验证手机号码
     *
     * @param mobile
     * @return
     */
    public static boolean validatePhone(String phone) {
        if (phone == null) {
            return false;
        }
        Pattern p = Pattern
                .compile("^((13[0-9])|(14[5-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$");
        Matcher m = p.matcher(phone);
        return m.matches();
    }

    /**
     * 验证是否是数字字符串
     *
     * @param str
     * @return
     */
    public static boolean validateNumberString(String numberString) {
        if (numberString == null) {
            return false;
        }
        Pattern p = Pattern.compile("^\\d+$");
        Matcher m = p.matcher(numberString);
        return m.matches();
    }

    /**
     * 验证Email
     *
     * @param email email地址，格式：zhangsan@sina.com，zhangsan@xxx.com.cn，xxx代表邮件服务商
     * @return 验证成功返回true，验证失败返回false
     */
    public static boolean validateEmail(String email) {
        Pattern p = Pattern.compile("\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?");
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * @param @param  string
     * @param @param  length
     * @param @return
     * @return
     * @throws
     * @Description: 判断字符串长度是否大于指定长度
     */
    public static boolean validateLengthLonger(String string, int length) {
        if (string == null) {
            return false;
        }

        return string.length() > length;
    }

    /**
     * @param @param  string
     * @param @param  length
     * @param @return
     * @return
     * @throws
     * @Description: 判断字符串长度是否大于小于指定长度
     */
    public static boolean validateLengthshorter(String string, int length) {
        if (string == null) {
            return false;
        }

        return string.length() < length;
    }

    /**
     * @param @param  a
     * @param @param  b
     * @param @return
     * @return
     * @throws
     * @Description: 判断a和b是否相等
     */
    public static boolean validateStringIsEquals(String a, String b) {
        if (a == b)
            return true;
        int length;
        if (a != null && b != null && (length = a.length()) == b.length()) {
            if (a instanceof String && b instanceof String) {
                return a.equals(b);
            } else {
                for (int i = 0; i < length; i++) {
                    if (a.charAt(i) != b.charAt(i))
                        return false;
                }
                return true;
            }
        }
        return false;
    }

    /**
     * 去掉url中的路径，留下请求参数部分
     *
     * @param strURL url地址
     * @return url请求参数部分
     * @author lzf
     */
    public static String TruncateUrlPage(String strURL) {
        String strAllParam = null;
        String[] arrSplit = null;
        strURL = strURL.trim();
        arrSplit = strURL.split("[?]");
        if (strURL.length() > 1) {
            if (arrSplit.length > 1) {
                for (int i = 1; i < arrSplit.length; i++) {
                    strAllParam = arrSplit[i];
                }
            }
        }
        return strAllParam;
    }

    /**
     * 解析出url参数中的键值对
     * 如 "index.jsp?Action=del&id=123"，解析出Action:del,id:123存入map中
     *
     * @param URL url地址
     * @return url请求参数部分
     * @author lzf
     */
    public static Map<String, String> urlSplit(String URL) {
        Map<String, String> mapRequest = new HashMap<String, String>();
        String[] arrSplit = null;
        String strUrlParam = TruncateUrlPage(URL);
        if (strUrlParam == null) {
            return mapRequest;
        }
        arrSplit = strUrlParam.split("[&]");
        for (String strSplit : arrSplit) {
            String[] arrSplitEqual = null;
            arrSplitEqual = strSplit.split("[=]");//解析出键值
            if (arrSplitEqual.length > 1) {//正确解析
                mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);
            } else {
                if (arrSplitEqual[0] != "") {//只有参数没有值，不加入
                    mapRequest.put(arrSplitEqual[0], "");
                }
            }
        }
        return mapRequest;
    }
}
